package aspectjutil.eventmanager;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.junit.Before;
import org.slf4j.Logger;

abstract public class EventHandlerTestTemplate extends TestCase {
	protected static Logger logger;
	
	protected ClassLoader childDelegationClassLoader;
	protected List<String> eventTraceList;
	protected List<Object> eventerTraceList;
	protected List<Object> eventResultTraceList;
	protected List<Class<?>> triggeredEventTraceList;
	
	@Before
	public void setUp() {
		// Initialize childDelegationClassLoader --------------------------------------------------
		try {
			childDelegationClassLoader = EventManagerTestUtils.childDelegationClassLoaderFactory();
		}
		catch( Throwable throwable) {
			String message = "Failure in initializing childDelegationClassLoader.";
			if ( logger.isErrorEnabled()) {
				logger.error( message, throwable);
			}
			Assert.fail( message + "\nCheck error log for detail.");
		}
		// ----------------------------------------------------------------------------------------
		
		Class<?> eventTraceSupporterClass = null;
		try {
			eventTraceSupporterClass 
			= EventManagerTestUtils.getEventTraceSupporterClass( childDelegationClassLoader);
		}
		catch( Throwable throwable) {
			String message = 
				String.format(
						"Failure in obtaining %1$s class.",
						EventManagerTestUtils.EventTraceSupporterClassName
						);
			if ( logger.isErrorEnabled()) {
				logger.error( message, throwable);
			}
			Assert.fail( message + "\nCheck error log for detail.");
		}
		
		// Initialize eventTraceList --------------------------------------------------------------
		eventTraceList = Collections.synchronizedList( new LinkedList<String>());
		try {
			EventManagerTestUtils.setEventTraceListOfEventTraceSupporter( 
					eventTraceSupporterClass, eventTraceList);
		}
		catch( Throwable throwable) {
			String message = 
				String.format(
						"Failure in initializing %1$s.eventTraceList member field.",
						EventManagerTestUtils.EventTraceSupporterClassName
						);
			if ( logger.isErrorEnabled()) {
				logger.error( message, throwable);
			}
			throw new RuntimeException( message + "\nCheck error log for detail.");
		}
		// ----------------------------------------------------------------------------------------
		
		// Initialize eventerTraceList ------------------------------------------------------------
		eventerTraceList = Collections.synchronizedList( new LinkedList<Object>());
		try {
			EventManagerTestUtils.setEventerTraceListOfEventTraceSupporter( 
					eventTraceSupporterClass, eventerTraceList);
		}
		catch( Throwable throwable) {
			String message = 
				String.format(
						"Failure in initializing %1$s.eventerTraceList member field.",
						EventManagerTestUtils.EventTraceSupporterClassName
						);
			if ( logger.isErrorEnabled()) {
				logger.error( message, throwable);
			}
			throw new RuntimeException( message + "\nCheck error log for detail.");
		}
		// ----------------------------------------------------------------------------------------
		
		// Initialize eventResultTraceList ------------------------------------------------------------
		eventResultTraceList = Collections.synchronizedList( new LinkedList<Object>());
		try {
			EventManagerTestUtils.setEventResultTraceListOfEventTraceSupporter( 
					eventTraceSupporterClass, eventResultTraceList);
		}
		catch( Throwable throwable) {
			String message = 
				String.format(
						"Failure in initializing %1$s.eventResultTraceList member field.",
						EventManagerTestUtils.EventTraceSupporterClassName
						);
			if ( logger.isErrorEnabled()) {
				logger.error( message, throwable);
			}
			throw new RuntimeException( message + "\nCheck error log for detail.");
		}
		// ----------------------------------------------------------------------------------------
		
		// Initialize triggeredEventTraceList -----------------------------------------------------
		triggeredEventTraceList = Collections.synchronizedList( new LinkedList<Class<?>>());
		try {
			EventManagerTestUtils.setTriggeredEventTraceListOfEventTraceSupporter( 
					eventTraceSupporterClass, triggeredEventTraceList);
		}
		catch( Throwable throwable) {
			String message = 
				String.format(
						"Failure in initializing %1$s.triggeredEventTraceList member field.",
						EventManagerTestUtils.EventTraceSupporterClassName
						);
			if ( logger.isErrorEnabled()) {
				logger.error( message, throwable);
			}
			throw new RuntimeException( message + "\nCheck error log for detail.");
		}
		// ----------------------------------------------------------------------------------------
	}

}
